package com.huawei.user.service.impl;

import com.huawei.basic.constant.BaseConstants;
import com.huawei.basic.dto.RegisterDto;
import com.huawei.basic.exception.BusinessException;
import com.huawei.basic.utils.MD5Utils;
import com.huawei.basic.utils.StrUtils;
import com.huawei.user.domain.Logininfo;
import com.huawei.user.domain.User;
import com.huawei.user.mapper.LogininfoMapper;
import com.huawei.user.mapper.UserMapper;
import com.huawei.user.service.IUserService;
import com.huawei.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
<<<<<<< HEAD
import org.springframework.transaction.annotation.Transactional;
=======
>>>>>>> 815a078707a159f81958d10b606db1f81992e9cf

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author raintea
 * @since 2022-05-09
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;
<<<<<<< HEAD
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
=======
    
    @Autowired
    private LogininfoMapper logininfoMapper;
    
    @Autowired
    private UserMapper userMapper;
    
    @Override
>>>>>>> 815a078707a159f81958d10b606db1f81992e9cf
    public void phoneRegister(RegisterDto dto) {
        String phone = dto.getPhone();
        String password = dto.getPassword();
        String verifyCodeValue = dto.getVerifyCodeValue();
<<<<<<< HEAD
        //1.校验
        //。空校验
        //。两次密码是否一致
        //。验证码是否有效
        String key = BaseConstants.SmsCodeConstants.BUSINESS_REGISTER_PREFIX+phone;
        Object smsCodeTmp = redisTemplate.opsForValue().get(key);
        if(smsCodeTmp == null){// 9527:46978465489741
            throw new BusinessException("短信验证码已失效，请重新获取！！！");
        }
        String smsCode = smsCodeTmp.toString().split(":")[0];
        if(!verifyCodeValue.equalsIgnoreCase(smsCode)){
            throw new BusinessException("验证码输入错误！！！！");
        }
        //。手机号不能被注册....@TODO
        //2.保存数据 t_user + t_logininfo
        User user = dto2User(dto);
        Logininfo logininfo = user2Logininfo(user);
        //。先保存t_loginiinfo  返回自增ID
        logininfoMapper.save(logininfo);
        //。设置loginingoId 并保存user
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);//一定要设置type
=======
        // 空校验
        // 验证码是否有效
        String key = BaseConstants.SmsCodeConstants.BUSINESS_REGISTER_PREFIX + phone;
        Object smsCodeTmp = redisTemplate.opsForValue().get(key);
        if (smsCodeTmp == null){
            throw new BusinessException("短信验证码失效，请重新获取！");
        }
        String smsCode =  smsCodeTmp.toString().split(":")[0];
        if (!verifyCodeValue.equalsIgnoreCase(smsCode)){
            throw new BusinessException("验证码输入错误，请重新输入！");
        }
        // 保存数据
        User user = dto2User(dto);
        Logininfo logininfo = user2Loginfo(user);
        
        // 保存logininfo数据，返回自增id
        logininfoMapper.save(logininfo);
        // 设置logininfoId，并保存user
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
        
    }

    private Logininfo user2Loginfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
>>>>>>> 815a078707a159f81958d10b606db1f81992e9cf
        return logininfo;
    }

    private User dto2User(RegisterDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
<<<<<<< HEAD
        String md5pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);//加密的时候使用的盐值保存到数据
        user.setPassword(md5pwd);//加密之后的密码，保存到数据库
        return user;
    }


    @Override
    public void update(User user) {
        super.update(user);
        //修改logininfo
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
        //删除logininfo
=======
        String pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);// 加密后的盐值
        user.setPassword(pwd);// 加密过后的密码
   
        return user;
        
    }

    /**
     * 修改logiinfo
     * @param user
     */
    @Override
    public void update(User user) {
        super.update(user);
    }
    /**
     * 删除logiinfo
     * @param id
     */
    @Override
    public void remove(Long id) {
        super.remove(id);
>>>>>>> 815a078707a159f81958d10b606db1f81992e9cf
    }
}
